import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app/bean/list_other_problem_entity.dart';
import 'package:flutter_app/values/MyColors.dart';
import 'package:flutter_app/widget/empty_view.dart';
import 'package:flutter_screenutil/src/size_extension.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import 'film_list_logic.dart';


/**
 * @Description: 随手拍上报记录 list
 * @Author: 下雨了
 * @CreateDate: 2022年5月31日 13:59:55
 * @Version: 1.0
 */
///
class FilmListPage extends StatefulWidget {
  final String status;

  const FilmListPage({
    Key? key,
    required this.status,
  }) : super(key: key);

  @override
  _FilmlistPageState createState() => _FilmlistPageState();
}


class _FilmlistPageState extends State<FilmListPage> {
  final logic = Get.put(FilmListLogic());
  final state = Get.find<FilmListLogic>().state;

  // 刷新控制器 initialRefresh 是否最初刷新
  late RefreshController refreshController = RefreshController(initialRefresh: false);

  @override
  void initState() {
    super.initState();
    logic.listOtherProblem(state.currentPage.value, widget.status);
  }

  /// 刷新
  void onRefresh() async {
    state.currentPage.value = 1;
    // 监视网络获取
    await Future.delayed(Duration(milliseconds: 1000));
    // 如果失败,使用refreshFailed ()
    logic.listOtherProblem(state.currentPage.value, widget.status).then((_) {
      refreshController.refreshCompleted();
      refreshController.resetNoData();
    });
  }

  /// 加载
  void onLoading() async {
    // 监视网络获取
    await Future.delayed(Duration(milliseconds: 1000));
    // 如果完成使用loadFailed()，如果没有数据返回，使用LoadNodata()
    int totalPages = state.listOtherProblemEntity?.totalPages ?? 1;

    if (state.currentPage.value < totalPages) {
      state.currentPage.value++;
      logic.listOtherProblem(state.currentPage.value, widget.status).then((_) {
        refreshController.loadComplete();
      });
    } else {
      refreshController.loadNoData();
    }
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<FilmListLogic>(builder: (logic) {
      return SmartRefresher(
        controller: refreshController,
        //  允许下拉刷新
        enablePullDown: true,
        // 允许上拉加载
        enablePullUp: true,
        // 刷新
        onRefresh: onRefresh,
        // 加载
        onLoading: onLoading,
        header: WaterDropHeader(complete: Text('刷新完成', style: TextStyle(color: MyColors.FF333333))),
        footer: ClassicFooter(loadingText: '加载中...', idleText: '上拉加载更多..', noDataText: '没有更多数据了'),
        child: (state.listOtherProblemEntity?.content?.length ?? 0) > 0 ? ListView.builder(
          padding: EdgeInsets.only(top: 16.r),
          shrinkWrap: true,
          //解决无线高度问题 解决SingleChildScrollView 和listview冲突问题 ListView.builder
          itemBuilder: (BuildContext context, int index) {
            return ListItem(state.listOtherProblemEntity!.content![index]);
          },
          itemCount: state.listOtherProblemEntity?.content?.length,
          physics: const BouncingScrollPhysics(),
        ) : EmptyView(),
      );
    });
  }

  Widget ListItem(ListOtherProblemContent item) {
    return Container(
      margin: EdgeInsets.only(left: 16.r, right: 16.r, bottom: 10.r),
      child: BrnShadowCard(
        circular: 4.r,
        spreadRadius: 1,
        padding: EdgeInsets.all(16.r),
        child: InkWell(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                item.title ?? "",
                maxLines: 2,
                textAlign: TextAlign.justify,
                overflow: TextOverflow.ellipsis,
                style: TextStyle(color: MyColors.FF333333, fontSize: 14.sp, fontWeight: FontWeight.w500),
              ),
              SizedBox(
                height: 8.r,
              ),
              Row(
                children: [
                  BrnTagCustom(
                    tagText: logic.getTagText(item.flowStatus ?? "0"),
                    textColor: logic.getTextColor(item.flowStatus ?? "0"),
                    backgroundColor: logic.getBackgroundColor(item.flowStatus ?? "0"),
                    fontSize: 11.sp,
                    textPadding: EdgeInsets.only(left: 3.5.r, right: 3.5.r, top: 1.5.r, bottom: 1.5.r),
                  ),
                  SizedBox(width: 10.5.r,),
                  Text(
                    item.createTime ?? "",
                    style: TextStyle(color: MyColors.FF999999, fontSize: 11.sp),),
                ],
              )
            ],
          ),
          onTap: () {
            logic.goToFilmDetail(item.id);
          },
        ),
      ),
    );
  }

  @override
  void dispose() {
    Get.delete<FilmListLogic>();
    super.dispose();
  }

}
